#include "command.h"

void command_ls(int netFD, MYSQL *conn, client_t *cl){
    char buf[1024] = { 0 };
    sprintf(buf, "select file_name from file where father_id = %d", cl->file_id);
    if (mysql_query(conn, buf)) {                
        printf("%s\n", mysql_error(conn));       
        return;                                    
    }                                                 
    MYSQL_RES *result;
    MYSQL_ROW row;
    result = mysql_store_result(conn);
    if(!result || (mysql_num_rows(result) == 0)){
        int num = -1;
        send(netFD, &num, sizeof(int), MSG_NOSIGNAL);
        return;
    }

    int num = 1;
    send(netFD, &num, sizeof(int), MSG_NOSIGNAL);

    int num_rows = mysql_num_rows(result);
    send(netFD, &num_rows, sizeof(int), MSG_NOSIGNAL);
    for(int i = 0; i < num_rows; i++){
        row = mysql_fetch_row(result);
        char buf1[128] = { 0 };
        strcpy(buf1, row[0]);
        send(netFD, buf1, sizeof(buf1), MSG_NOSIGNAL);
    }
    mysql_free_result(result);

}
